package com.zqh.shoppingcartquery.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zqh.myfmmall.clients.ProductImgQueryClient;
import com.zqh.myfmmall.clients.ProductQueryClient;
import com.zqh.myfmmall.clients.ProductSkuQueryClient;
import com.zqh.myfmmall.entity.Product;
import com.zqh.myfmmall.entity.ProductImg;
import com.zqh.myfmmall.entity.ProductSku;
import com.zqh.myfmmall.entity.ShoppingCart;
import com.zqh.myfmmall.result.Result;
import com.zqh.myfmmall.vo.ShoppingCartVO;
import com.zqh.shoppingcartquery.dao.ShoppingCartQueryDao;
import com.zqh.shoppingcartquery.service.ShoppingCartQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-02-18 2:19
 */
@Service("shoppingCartQueryService")
public class ShoppingCartQueryServiceImpl extends ServiceImpl<ShoppingCartQueryDao, ShoppingCart> implements ShoppingCartQueryService{
    @Autowired
    private ProductSkuQueryClient productSkuQueryClient;
    @Autowired
    private ProductQueryClient productQueryClient;
    @Autowired
    private ProductImgQueryClient productImgQueryClient;


    @Override
    public List<ShoppingCartVO> getVosByUserId(String userId) {
        ArrayList<ShoppingCartVO> shoppingCartVOS = new ArrayList<>();
        List<ShoppingCart> shoppingCarts = query().eq("user_id", userId).list();
        List<String> skuIds = shoppingCarts.stream().map(shoppingCart -> shoppingCart.getSkuId()).collect(Collectors.toList());
        Result skusResult = productSkuQueryClient.getByIds(skuIds);
        List<ProductSku> productSkus = JSON.parseArray(JSON.toJSONString(skusResult.getData()),ProductSku.class);
        for (int i = 0; i < shoppingCarts.size(); i++) {
            ShoppingCart shoppingCart = shoppingCarts.get(i);
            String productId = shoppingCart.getProductId();
            //购物车可能有同一个商品的不同套餐，所以不能通过id来batch查询，只能这样一个一个查询
            Result productResult = productQueryClient.getById(productId);
            Product product = JSON.parseObject(JSON.toJSONString(productResult.getData()),Product.class);
            ProductSku productSku = productSkus.get(i);
            Result mainImgResult = productImgQueryClient.getMainImg(productId);
            String mainImgUrl = ((String) mainImgResult.getData());
            ShoppingCartVO shoppingCartVO = new ShoppingCartVO(shoppingCart, productSku, product, mainImgUrl);
            shoppingCartVOS.add(shoppingCartVO);
        }
        return shoppingCartVOS;
    }

    @Override
    public List<ShoppingCartVO> getVosByIds(List<String> cartIds) {
        ArrayList<ShoppingCartVO> shoppingCartVOS = new ArrayList<>();
        //直接用listByIds内部是用in构造sql，可能导致顺序对不上，可改为一个一个查找（但是这样效率低）
        List<ShoppingCart> shoppingCarts = listByIds(cartIds);
        List<String> skuIds = shoppingCarts.stream().map(shoppingCart -> shoppingCart.getSkuId()).collect(Collectors.toList());
        Result skusResult = productSkuQueryClient.getByIds(skuIds);
        List<ProductSku> productSkus = JSON.parseArray(JSON.toJSONString(skusResult.getData()),ProductSku.class);
        for (int i = 0; i < shoppingCarts.size(); i++) {
            ShoppingCart shoppingCart = shoppingCarts.get(i);
            String productId = shoppingCart.getProductId();
            //购物车可能有同一个商品的不同套餐，所以不能通过id来batch查询，只能这样一个一个查询
            Result productResult = productQueryClient.getById(productId);
            Product product = JSON.parseObject(JSON.toJSONString(productResult.getData()),Product.class);
            ProductSku productSku = productSkus.get(i);
            Result mainImgResult = productImgQueryClient.getMainImg(productId);
            String mainImgUrl = ((String) mainImgResult.getData());
            ShoppingCartVO shoppingCartVO = new ShoppingCartVO(shoppingCart, productSku, product, mainImgUrl);
            shoppingCartVOS.add(shoppingCartVO);
        }
        return shoppingCartVOS;
    }

}
